package v1

import (
	"team-evaluate/global"
	"team-evaluate/internal/model"
	"team-evaluate/internal/service"
	"team-evaluate/pkg/app"
	"team-evaluate/pkg/convert"
	"team-evaluate/pkg/errcode"
	"team-evaluate/pkg/util"

	"github.com/gin-gonic/gin"
)

type User struct{}

func NewUser() User {
	return User{}
}

// @Summary 获取多个用户
// @Produce  json
// @Tags 用户管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param page query int false "页码"
// @Param page_size query int false "每页数量"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/users [get]
func (u User) List(c *gin.Context) {
	response := app.NewResponse(c)
	svc := service.New(c.Request.Context())
	pager := app.Pager{Page: app.GetPage(c), PageSize: app.GetPageSize(c)}

	p, err1 := util.StrutToString(pager)
	if err1 == nil {
		c.Set("request_params", p)
	}

	totalRows, users, err := svc.GetUserList(&pager)
	if err != nil {
		response.ToErrorResponse(err)
		return
	}

	response.ToResponseList(users, totalRows)
}

// @Summary 根据id获取用户
// @Produce  json
// @Tags 用户管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int false "id"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user/${id} [get]
func (u User) FindById(c *gin.Context) {
	param := model.FindUserByIdRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	user, err := svc.FindUserById(&param)
	if err != nil {
		global.Logger.Errorf("svc.FindUserById err: %v", err)
		response.ToErrorResponse(errcode.ErrorFindUserByIdFail.WithDetails(err.Error()))
		return
	}
	response.ToResponseObj(user)
	return
}

// @Summary 新增用户
// @Produce  json
// @Tags 用户管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param name body string true "用户名称" maxlength(100)
// @Param email body string true "邮箱"
// @Param password body string true "密码" minlength(6)
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user [post]
func (u User) Create(c *gin.Context) {
	param := model.CreateUserRequest{}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())

	err = svc.CreateUser(&param)
	if err != nil {
		global.Logger.Errorf("svc.CreateUser err: %v", err)
		response.ToErrorResponse(errcode.ErrorCreateUserFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}

// @Summary 更新用户
// @Produce  json
// @Tags 用户管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int true "用户 ID"
// @Param name body string false "用户名称" maxlength(100)
// @Param email body string false "用户邮箱"
// @Param password body string false "密码"
// @Param age body int false "年龄"
// @Param gender body string false "性别"
// @Param desc body string false "描述" maxlength(1000)
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user/{id} [put]
func (u User) Update(c *gin.Context) {
	param := model.UpdateUserRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.UpdateUser(&param)
	if err != nil {
		global.Logger.Errorf("svc.UpdateUser err: %v", err)
		response.ToErrorResponse(errcode.ErrorUpdateUserFail.WithDetails(err.Error()))
		return
	}
	response.ToResponse(nil)
	return
}

// @Summary 删除用户
// @Produce  json
// @Tags 用户管理
// @Param Authorization header string true "Bearer 用户令牌"
// @Param id path int true "用户 ID"
// @Success 200 {object} app.HttpSuccessResult "成功"
// @Failure 400 {object} errcode.Error "请求错误"
// @Failure 500 {object} errcode.Error "内部错误"
// @Router /api/v1/user/{id} [delete]
func (u User) Delete(c *gin.Context) {
	param := model.DeleteUserRequest{ID: convert.StrTo(c.Param("id")).MustUInt32()}
	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &param)
	if !valid {
		global.Logger.Errorf("app.BindAndValid errs: %v", errs)
		response.ToErrorResponse(errcode.InvalidParams.WithDetails(errs.Errors()...))
		return
	}

	p, err := util.StrutToString(param)
	if err == nil {
		c.Set("request_params", p)
	}

	svc := service.New(c.Request.Context())
	err = svc.DeleteUser(&param)
	if err != nil {
		global.Logger.Errorf("svc.DeleteUser err: %v", err)
		response.ToErrorResponse(errcode.ErrorDeleteUserFail.WithDetails(err.Error()))
		return
	}

	response.ToResponse(nil)
}
